<?php
/**
 * Created by PhpStorm.
 * User: hpp
 * Date: 2019/12/17
 * Time: 17:00
 */
namespace gzapi\common\model;

use think\Model;

/**
 * 分公司
 * Class RlCompanyModel
 * @package api\common\model
 */
class RlCompanyModel extends Model
{
    protected $pk = 'company_id';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    const LIMIT = 10;

    /*
     * 获取分公司
     */
    public static function getCompany(){
        $data = self::field('company_id,fgs,fgsmc')->select()->toArray();
        foreach ($data as $k=>&$v){
            $v['jxs_num'] = RlEtCompanyModel::where('flag',1)->where('fgs',$v['fgs'])->count();
        }
        return $data;
    }

    /**
     * 获取经销商
     * $fgscode 分公司编码
     */
    public static function getJxs($fgscode){
        $jxsbms = RlEtCompanyModel::where('flag',1)->where('fgs',$fgscode)->column('jxsbm');
        $data = RlEtCustomerModel::where('jxsbm','in',$jxsbms)->where('dj_flag',1)->field('et_customer_id,jxsbm,jxs_mc,jxs_level')->select();
        foreach ($data as $k=>&$v){
            // 每个经销商下有几个安装队
            # 所有经销商下的安装商编码
            $serverCode = RlEtCustInstalRelModel::where(['jxsbm' => $v['jxsbm'],'flag' => 1])->column('server_code');
            $v['azd_num'] = RlEtAzdModel::where('server_code','in',$serverCode)->where('active_flag','>',0)->count();
        }
        return $data;
    }

    /**
     * 获取安装队
     * $jxsbm  经销商编码
     * $page 页数
     */
    public static function getAzd($jxsbm){
# 所有经销商下的安装商编码
        $serverCode = RlEtCustInstalRelModel::where(['jxsbm' => $jxsbm,'flag' => 1])->column('server_code');
        # 安装商-->安装队
        $data = RlEtAzdModel::where('server_code','in',$serverCode)->where('active_flag','>',0)->field('et_azd_id,server_code,engineer_code,engineer_name')->select()->toArray();
        return $data;
    }

    /**
     * 获取经销商下的安装队
     * @param $code     编码
     * @param $type     1总部 2分公司 3经销商
     * @return array
     */
    public static function getAllCode($code, $type){
        if (!in_array($type,[1,2,3]))  return [];
        if ($type == 1){
        }elseif ($type == 2){
            $jxsbms = RlEtCompanyModel::where('flag',1)->where('fgs',$code)->column('jxsbm');
            return $jxsbms;
        }
    }
}